// https://codeforces.com/gym/104369/problem/C
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define debug(x) cout << #x << " = " << x << '\n'
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<pair<int, int>> shop;
    for (int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        shop.push_back(make_pair(a, b));
    }
    sort(shop.begin(), shop.end());
    int left = 0, right = n - 1;
    int buy = 0, sell = 0;
    if (shop[left].first == shop[right].first)
    {
        cout << 0 << endl;
        return;
    }
    while (left < right)
    {
        int m = min(shop[left].second, shop[right].second);
        buy += shop[left].first * m;
        sell += shop[right].first * m;
        shop[left].second -= m;
        shop[right].second -= m;
        if (shop[left].second == 0)
            left++;
        if (shop[right].second == 0)
            right--;
    }
    cout << sell - buy << endl;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}